<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Digital</em>
</p>
<p class='vspace'></p><h3>Button State Change Detection (Edge Detection)</h3>
<p>This program checks when the button changes state from off to on, and counts how many times this change of state happens. This is called state change detection or edge detection.
</p>
<p class='vspace'></p><div  class='BOM' >
<h3>Hardware Required</h3>
<ul><li>MSP-EXP430G2 LaunchPad
</li><li>pushbutton [available already on-board as part of the LaunchPad]
</li><li>LED [available already on-board as a part of the Launchpad]
</li><li>momentary button or switch
</li><li>10K ohm resistor
</li><li>breadboard
</li><li>hook-up wire
</li></ul><p class='vspace'></p></div>
<h3>Relevant Groundwork</h3>
<p class='vspace'></p><p>In order detect the state change, the current button state is compared with the last button state and if the current button state is high, then the button changed from off to on. A counter is incremented whenever this state change happens. The button push counter's value is checked, and if it's an even multiple of four, it turns the LED on pin 14 ON. Otherwise, it turns it off.
</p>
<p class='vspace'></p><h3>Circuit</h3>
<div  class='circuit' >
<div><img height='250px' src='../reference/img/Button_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p><p class='vspace'></p></div>
<u>For on-board push button</u>
<p>No external circuitry is required. 
</p>
<u>For external push button</u>
<p>Connect three wires to the Launch pad board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 3 volt supply and ground. The third wire goes from digital pin p1.3 to one leg of the pushbutton. That same leg of the button connects through a pull-up resistor (here 10 K Ohms) to Vcc. The other leg of the button connects to the ground.
</p>
<p class='vspace'></p><h3>Schematic</h3>
<div  class='circuit' >
<div><img height='400px' src='../reference/img/Button_schem.png' alt='' title='' /></div>
</div>
<p class='vspace'></p><h3>Code Explanation</h3>
<p>In the program, pin 14, which has green LED connected to it, is set up as output in the setup function. 
</p>
<code>pinMode(ledPin, OUTPUT);</code>
<p class='vspace'></p>
<p>Here the ledpin refers to the GREEN_LED which is set as output. The push button which is connected to pin 5 is set up as input. The pull-up resistor connected to this pin is enabled.
</p>
<code>pinMode(buttonPin, INPUT_PULLUP);</code>
<p class='vspace'></p>
<p>Begin serial communications, at 9600 bits of data per second, between your Launchpad and your computer with the line
</p>
<code>Serial.begin(9600);</code>
<p class='vspace'></p>
<p>In the main loop, the state of the pushbutton is saved in the variable “ButtonState”. 
</p>
<code>int ButtonState = digitalRead(buttonPin);</code>
<p class='vspace'></p>
<p>If the state of the pushbutton has changed from its previous stored state
</p>
<code>if (ButtonState != lastButtonState)</code>
<p class='vspace'></p>
<p>If the ButtonState is High, then the ButtonPush counter is incremented and the value is output to serial terminal. Every time the value of the counter is checked, if it has reached 4. If the ButtonPush counter reaches a value of 4, then the LED is turned ON.
</p>
<pre>
if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
} else {
   digitalWrite(ledPin, LOW);
}
</pre>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  State change detection (edge detection)
    
  Often, you don't need to know the state of a digital input all the time,
  but you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON.  This is called
  state change detection, or edge detection.
  
  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe
  modified 27 Apr 2012
  Robert Wessels

  This example code is in the public domain.
*/
const int  buttonPin = PUSH2;    // the pin that the pushbutton is attached to
const int ledPin = RED_LED;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
  
}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
<p>- use the button to print something in binary to the serial monitor
</p>
<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='PinMode.html'>pinMode</a>()
</li><li><a class='wikilink' href='DigitalWrite.html'>digitalWrite</a>()
</li><li><a class='wikilink' href='DigitalRead.html'>digialRead</a>()
</li><li><a class='wikilink' href='If.html'>if</a>()
</li><li><a class='wikilink' href='Millis.html'>millis</a>()
</li><li><a class='wikilink' href='Tutorial_BareMinimum.html'>BareMinimum</a>: The bare minimum of code needed to start an Energia sketch.
</li><li><a class='wikilink' href='Tutorial_Blink.html'>Blink</a>: Turn an LED on and off.
</li><li><a class='wikilink' href='Tutorial_DigitalReadSerial.html'>DigitalReadSerial</a>: Read a switch, print the state out to the Energia Serial Monitor.
<</li><li><a class='wikilink' href='Tutorial_BlinkWithoutDelay.html'>BlinkWithoutDelay</a>:  Blinking an LED without using the delay() function
</li></ul>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
